package com.aaa.system.service.impl;

import com.aaa.entity.User;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.UserService;
import com.aaa.system.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @program: aaa-his-parent
 * @description:
 * @author: 闫克起2
 * @create: 2023-12-07 12:02
 **/
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User findUserByPhone(String phone) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("phone",phone);
        wrapper.eq("status",0);
        wrapper.eq("del_flag",0);
        User user = userDao.selectOne(wrapper);
        return user;
    }

    @Override
    public Page<User> selectUserByCond(UserVo userVo) {
        Page<User> page=new Page<>(userVo.getCurrent(),userVo.getPageSize());
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(Objects.nonNull(userVo.getDeptId())){
            wrapper.eq("dept_id",userVo.getDeptId());
        }
        if(StringUtils.hasText(userVo.getUserName())){
            wrapper.like("user_name",userVo.getUserName());
        }
        if(StringUtils.hasText(userVo.getPhone())){
            wrapper.eq("phone",userVo.getPhone());
        }
        if(Objects.nonNull(userVo.getStatus())){
            wrapper.eq("status",userVo.getStatus());
        }
        if(Objects.nonNull(userVo.getDateRange())){
             wrapper.between("create_time",userVo.getDateRange()[0],userVo.getDateRange()[1]);
        }
        Page<User> page1 = userDao.selectPage(page, wrapper);
        return page1;
    }

    @Override
    public List<User> findSchedulingUser() {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("scheduling_flag",0);
        wrapper.eq("del_flag",0);
        List<User> users = userDao.selectList(wrapper);
        return users;
    }
    @Override
    public List<User> findUser(Map<String, Object> params) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("scheduling_flag",0);
        wrapper.eq("del_flag",0);
        if(Objects.nonNull(params.get("userId"))){
        wrapper.eq("user_id",params.get("userId"));
        }
        if(Objects.nonNull(params.get("deptId"))){
        wrapper.eq("dept_id",params.get("deptId"));
        }
        List<User> users = userDao.selectList(wrapper);
        return users;
    }
}
